<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *配送方式
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
			array('op'=>'list','title'=>'配送方式列表'),
			array('op'=>'edit','title'=>'添加配送方式'),
	);

//配送方式
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','paixu','type','shouprice','xuprice','areas','shouweight','xuweight','status','isdefault');
	$list=pdo_getslice('jy_weishop_dispatch',$params,array($pindex,$psize),$total,$fields,'','paixu desc');
	if($list){
		foreach($list as &$item){
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]:设置满额包邮，请移步：活动营销--满额包邮--设置','edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'顺序','type'=>'string'),
			array('name'=>'title','title'=>'名称','type'=>'string'),
			array('name'=>'type','title'=>'计费方式','type'=>'radio','options'=>'type'),
			//array('name'=>'shouprice','title'=>'首重(件)价格','type'=>'string'),
			//array('name'=>'xuprice','title'=>'续重(件)价格','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'isdefault','title'=>'是否默认','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'select','options'=>'statuss','select_title'=>'请搜索状态'),
		)
	);
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$return['statuss']=array(array('id'=>0,'title'=>'禁用'),array('id'=>1,'title'=>'启用'));
	$return['type']=array('0'=>'按重量计费','1'=>'按件计费');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_dispatch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array('nodispatchareas'=>2);
	}
	$return['item']=$item;
	$return['item']['areas']=json_decode($return['item']['areas']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回配送方式列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'tip','title'=>'[温馨提示]：<red>商品那边选择配送模板的时候，尽量设置统一的默认模板，这样减少运费重复计算</red>！'),
					array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>''),
					array('type'=>'string','title'=>'配送方式名称','name'=>'title','help'=>''),
					array('type'=>'switch','title'=>'是否默认','name'=>'isdefault','help'=>''),
					array('type'=>'radio','title'=>'计费方式','name'=>'type','help'=>'','options'=>'type'),
					//array('type'=>'unitstring','title'=>'单品满额包邮','name'=>'fullmoney','unit'=>'元','help'=>'满多少元包邮！<red>因为不同商品可以选择不同的配送模板，所以只对单品有效</red>！'),
					//array('type'=>'unitstring','title'=>'单品满件包邮','name'=>'fullnum','unit'=>'件','help'=>'满多少元件邮！<red>因为不同商品可以选择不同的配送模板，所以只对单品有效</red>！'),
					array('type'=>'select','title'=>'物流公司','name'=>'express','options'=>'express','option_title'=>'name','help'=>''),
					array('type'=>'radio','title'=>'配送区域类型','name'=>'isdispatcharea','help'=>'','options'=>'isdispatcharea','help'=>'不配送区域:不配送选择的区域,只配送未选择的区域;只配送区域:只配送选择的区域,不配送未选择的区域'),
					array(
						'type'=>'array',
						'name'=>'areas',
						'show'=>array('isdispatcharea'=>2),
						'add_tip'=>'添加一个配送区域',
						'list'=>array(
							array('type'=>'string','title'=>'配送区域','name'=>'area','help'=>'点击右侧按钮选择区域'),
							array('type'=>'radio','title'=>'计费方式','name'=>'type','help'=>'','options'=>'type'),
							array('type'=>'string','name'=>'shou','title'=>'首重(KG)/首件(件)'),
							array('type'=>'string','name'=>'shouprice','title'=>'首费(元)'),
							array('type'=>'string','name'=>'xu','title'=>'续重(KG)/续件(件)'),
							array('type'=>'string','name'=>'xuprice','title'=>'续费(元)'),
						),
						'details'=>array(
							array('type'=>'choose','title'=>'配送区域','name'=>'area','help'=>'点击右侧按钮选择区域','op'=>'choose'),
							array('type'=>'radio','title'=>'计费方式','name'=>'type','help'=>'','options'=>'type'),
							array('type'=>'string','name'=>'shou','title'=>'首重(KG)/首件(件)'),
							array('type'=>'string','name'=>'shouprice','title'=>'首费(元)'),
							array('type'=>'string','name'=>'xu','title'=>'续重(KG)/续件(件)'),
							array('type'=>'string','name'=>'xuprice','title'=>'续费(元)'),
						)
					),
					array('type'=>'choose','title'=>'不配送区域','name'=>'nodispatchareas','help'=>'点击右侧按钮选择区域','op'=>'choose','show'=>array('isdispatcharea'=>0)),
					array('type'=>'choose','title'=>'只配送区域','name'=>'onlydispatchareas','help'=>'点击右侧按钮选择区域','op'=>'choose','show'=>array('isdispatcharea'=>1)),
					array('type'=>'string','name'=>'shouweight','title'=>'首重(KG)/首件(件)','show'=>array('isdispatcharea !='=>2)),
					array('type'=>'string','name'=>'shouprice','title'=>'首费(元)','show'=>array('isdispatcharea !='=>2)),
					array('type'=>'string','name'=>'xuweight','title'=>'续重(KG)/续件(件)','show'=>array('isdispatcharea !='=>2)),
					array('type'=>'string','name'=>'xuprice','title'=>'续费(元)','show'=>array('isdispatcharea !='=>2)),
					array('type'=>'switch','title'=>'状态','name'=>'status','help'=>''),
				)
			),
		)
	);
	
	$return['type']=array('0'=>'按重量计费','1'=>'按件计费');
	$return['isdispatcharea']=array('0'=>'不配送区域','1'=>'只配送区域','2'=>'自定义配送区域');
	$return['express']=pdo_getall('jy_weishop_express');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'paixu'=>intval($postdata['paixu']),
		'title'=>trim($postdata['title']),
		'isdefault'=>intval($postdata['isdefault']),
		'type'=>intval($postdata['type']),
		'express'=>intval($postdata['express']),
		'isdispatcharea'=>intval($postdata['isdispatcharea']),
		'nodispatchareas'=>trim($postdata['nodispatchareas']),
		'onlydispatchareas'=>trim($postdata['onlydispatchareas']),
		'areas'=>ijson_encode($postdata['areas']),
		'shouweight'=>intval($postdata['shouweight']),
		'shouprice'=>round($postdata['shouprice'],2),
		'xuweight'=>intval($postdata['xuweight']),
		'xuprice'=>round($postdata['xuprice'],2),
		'status'=>intval($postdata['status']),
		'fullnum'=>intval($postdata['fullnum']),
		'fullmoney'=>intval($postdata['fullmoney']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_dispatch',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_dispatch',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_dispatch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='choose'){
	$areas=file_get_contents(ROOT_D.'/core/resource/area.txt');
	$areas=unserialize($areas);
	$return['areas']=$areas;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的配送方式！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_weishop_dispatch',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isdefault'])){
		pdo_update('jy_weishop_dispatch',array('isdefault'=>intval($post['isdefault'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}